package wo.psys.springboot.generator;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import wo.common.util.WoJavaDocUtil;
import wo.common.util.WoUtil;
import wo.common.util.WoVelocityUtil;
import wo.jpa.reader.entity.WoJPAEntity;
import wo.jpa.reader.util.JPAReaderUtil;

/**
 * @author cailei
 * @date Nov 3, 2018
 */
public class PSysGenerator {

	private final static Logger LOG = LogManager.getLogger(PSysGenerator.class);

	/**
	 * @param projectDir
	 * @return
	 */
	private static String getSrcDir(String projectDir) {
		return WoUtil.getDirEndWithFileSeparator(projectDir) + "src/main/java/";
	}
	
	/**
	 * @param projectDir
	 * @return
	 */
	private static String getResourceDir(String projectDir) {
		return WoUtil.getDirEndWithFileSeparator(projectDir) + "src/main/resources/";
	}
	
	/**
	 * @param projectDir
	 * @param entityClass
	 */
	public static void createCode(String projectDir, String entityClass) {
		LOG.info("createCode:projectDir-" + projectDir + ",entityClass-" + entityClass);
		String srcDir = getSrcDir(projectDir);
		// 设置java源代码目录
		WoJavaDocUtil.setSrcPath(srcDir);
		// 构造实体数据
		WoJPAEntity e = new WoJPAEntity(entityClass);
		if (e.isIgnore()) {
			LOG.info("ignore entityClass-" + entityClass);
			return;
		}
		// 创建*Dto类
		createCode(srcDir, e, "dto", true);
		// 创建*Repository接口
		createCode(srcDir, e, "repository", false);
		// 创建*Service接口
		createCode(srcDir, e, "service", false);
		// 创建*ServiceImpl类
		createCode(srcDir, e, "service.impl", false);
		// 创建*Controller类
		createCode(srcDir, e, "controller", false);
	}

	/**
	 * @param projectDir
	 * @param entityClass
	 */
	public static void createHtml(String projectDir, String entityClass) {
		LOG.info("createHtml:projectDir-" + projectDir + ",entityClass-" + entityClass);
		String srcDir = getSrcDir(projectDir);
		// 设置java源代码目录
		WoJavaDocUtil.setSrcPath(srcDir);
		// 构造实体数据
		WoJPAEntity e = new WoJPAEntity(entityClass);
		if (e.isIgnore()) {
			LOG.info("ignore entityClass-" + entityClass);
			return;
		}
		String rootDir = getResourceDir(projectDir);
		// 创建文件所在目录
		String fileDir = rootDir + "templates/" + e.getModule() + "/";
		File dir = new File(fileDir);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		// 构造velocity数据
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("entity", e);
		// 设置文件路径
		String filePath = fileDir + e.getEntityNameLowerFirstChar() + ".html";
		// 根据模板生成文件
		WoVelocityUtil.mergeToFile("entity.html", filePath, map);
		// 设置文件路径
		filePath = fileDir + e.getEntityNameLowerFirstChar() + "-list.html";
		// 根据模板生成文件
		WoVelocityUtil.mergeToFile("entity-list.html", filePath, map);
		// 设置文件路径
		filePath = fileDir + e.getEntityNameLowerFirstChar() + "-selector.html";
		// 根据模板生成文件
		WoVelocityUtil.mergeToFile("entity-selector.html", filePath, map);
		// 设置文件路径
		filePath = fileDir + e.getEntityNameLowerFirstChar() + "-selector-list.html";
		// 根据模板生成文件
		WoVelocityUtil.mergeToFile("entity-selector-list.html", filePath, map);
		// 设置文件路径
		filePath = fileDir + e.getEntityNameLowerFirstChar() + "-create.html";
		// 根据模板生成文件
		WoVelocityUtil.mergeToFile("entity-create.html", filePath, map);
		// 设置文件路径
		filePath = fileDir + e.getEntityNameLowerFirstChar() + "-update.html";
		// 根据模板生成文件
		WoVelocityUtil.mergeToFile("entity-update.html", filePath, map);

		fileDir = rootDir + "static/js/" + e.getModule() + "/";
		dir = new File(fileDir);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		// 设置js文件路径
		filePath = fileDir + e.getEntityNameLowerFirstChar() + ".js";
		// 根据模板生成文件
		WoVelocityUtil.mergeToFile("entity.js", filePath, map);
		// 设置js文件路径
		filePath = fileDir + e.getEntityNameLowerFirstChar() + "Selector.js";
		// 根据模板生成文件
		WoVelocityUtil.mergeToFile("entitySelector.js", filePath, map);
		LOG.info("createHtml:" + entityClass + " OK.");
	}

	/**
	 * @param pkgName
	 * @return
	 */
	private static String getClassSuffix(String pkgName) {
		String[] pkgs = pkgName.split("[.]");
		StringBuffer sb = new StringBuffer();
		for (String pkg : pkgs) {
			sb.append(WoUtil.getUpperFirstChar(pkg));
		}
		return sb.toString();
	}

	/**
	 * @param rootDir
	 * @param e
	 * @param pkgName
	 * @param hasParent
	 */
	public static void createCode(String rootDir, WoJPAEntity e, String pkgName, Boolean hasParent) {
		rootDir = WoUtil.getDirEndWithFileSeparator(rootDir);
		LOG.info("createCode:" + pkgName + "...");
		// 创建文件所在目录
		String fileDir = rootDir + e.getSiblingPackage(pkgName).replace(".", "/") + "/";
		File dir = new File(fileDir);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		// 构造velocity数据
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("entity", e);
		// 设置文件路径
		String filePath = fileDir + e.getEntityName() + getClassSuffix(pkgName) + ".java";
		// 根据模板生成文件
		WoVelocityUtil.mergeToFile(pkgName + ".java", filePath, map);
		if (hasParent) {
			// 设置文件路径
			filePath = fileDir + e.getEntityName() + getClassSuffix(pkgName) + "_" + ".java";
			// 根据模板生成文件
			WoVelocityUtil.mergeToFile(pkgName + "_.java", filePath, map);
		}
		LOG.info("createCode:" + pkgName + " OK.");
	}

	public static void main(String[] args) {
//		JPAReaderUtil.findEntitiesByPackage("../wo-pintuer-springboot/", "com.qfedu.department.po", (projectDir, entityCls) -> {
//			createCode(projectDir, entityCls);
//			createHtml(projectDir, entityCls);
//		});
//		JPAReaderUtil.findEntitiesByPackage("../wo-pintuer-springboot/", "com.qfedu.exam.po", (projectDir, entityCls) -> {
//			createCode(projectDir, entityCls);
//			createHtml(projectDir, entityCls);
//		});
//		JPAReaderUtil.findEntitiesByPackage("../wo-pintuer-springboot/", "com.qfedu.school.po", (projectDir, entityCls) -> {
//			createCode(projectDir, entityCls);
//			createHtml(projectDir, entityCls);
//		});
		createCode("C:/work/codes/cd1806/wo-pexam2/", "com.qfedu.school.po.StudentAnswerJudgeRecord");
		// createCode("../wo-pintuer-springboot/", "com.qfedu.exam.po.ExerciseAnswer");
		createHtml("C:/work/codes/cd1806/wo-pexam2/", "com.qfedu.school.po.StudentAnswerJudgeRecord");
	}

}
